<template>
  <div class="mod-live-liveProdStore">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
    >
      <template slot="menuLeft">
        <el-button
          icon="el-icon-plus"
          type="primary"
          size="small"
          v-if="isAuth('live:liveProdStore:save')"
          @click="addOrUpdateHandle()"
        >
          {{ $t("crud.addTitle") }}
        </el-button>
        <span style="color: red"
          >{{ $t("live.numNewLiveProsRem") }}：{{ shopNum
          }}{{ $t("live.times") }},{{ $t("live.subPlatRemLimi") }}{{ totalNum
          }}{{ $t("live.times") }}</span
        >
      </template>
      <template slot-scope="scope" slot="coverPic" class="prod-image">
        <img :src="scope.row.coverPic" width="60" height="60" />
        <span class="name">{{ scope.row.name }}</span>
      </template>
      <template slot-scope="scope" slot="status">
        <el-tag size="mini" v-if="scope.row.status === -1">{{
          $t("live.itemDeleted")
        }}</el-tag>
        <el-tag size="mini" v-if="scope.row.status === 0">{{
          $t("productComm.waitPass")
        }}</el-tag>
        <el-tag size="mini" v-if="scope.row.status === 1">{{
          $t("station.underReview")
        }}</el-tag>
        <el-tag size="mini" v-if="scope.row.status === 2">{{
          $t("productComm.pass")
        }}</el-tag>
        <el-tag size="mini" v-if="scope.row.status === 3">{{
          $t("productComm.noPass")
        }}</el-tag>
        <el-tag size="mini" v-if="scope.row.status === 4">{{
          $t("product.violation")
        }}</el-tag>
        <el-tag size="mini" v-if="scope.row.status === 5">{{
          $t("live.platformOffShelf")
        }}</el-tag>
      </template>
      <template slot-scope="scope" slot="price">
        <div v-if="scope.row.priceType == 1">
          <span>￥{{ scope.row.price }} </span>
        </div>
        <div v-if="scope.row.priceType == 2">
          <span
            >￥{{ scope.row.price }} {{ $t("admin.dollar") }} - ￥{{
              scope.row.price2
            }}
            {{ $t("admin.dollar") }}</span
          >
        </div>
        <div v-if="scope.row.priceType == 3">
          <span
            >{{ $t("live.marketPrice") }} ￥{{ scope.row.price }}
            {{ $t("admin.dollar") }}</span
          >
          <br />
          <span
            >{{ $t("live.currentPrice") }} ￥{{ scope.row.price2 }}
            {{ $t("admin.dollar") }}</span
          >
        </div>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          v-if="isAuth('live:liveProdStore:update') && scope.row.status === 0"
          icon="el-icon-edit"
          size="small"
          type="text"
          @click="addOrUpdateHandle(scope.row.liveProdStoreId)"
          >{{ $t("crud.updateBtn") }}
        </el-button>
        <el-button
          v-if="scope.row.status !== 0"
          icon="el-icon-edit"
          size="small"
          type="text"
          @click="addOrUpdateHandle(scope.row.liveProdStoreId)"
          >{{ $t("live.view") }}
        </el-button>
        <!--
        <el-button
          v-if="scope.row.status === 2 || scope.row.status === 5"
          icon="el-icon-edit"
          size="small"
          type="text"
          @click="addOrUpdateHandle(scope.row.liveProdStoreId)"
        >更新信息</el-button>-->

        <el-button
          v-if="isAuth('live:liveProdStore:submitVerify') && (scope.row.status === 0 || scope.row.status === 5)"
          icon="el-icon-edit"
          size="small"
          type="text"
          @click="submitVerify(scope.row.liveProdStoreId)"
          >{{
            scope.row.status === 5
              ? $t("live.reReview")
              : $t("live.submitReview")
          }}
        </el-button>
        <el-button
          :disabled="scope.row.status === 1"
          type="text"
          icon="el-icon-delete"
          size="small"
          style="color: red"
          v-if="isAuth('live:liveProdStore:delete')"
          @click="deleteHandle(scope.row.liveProdStoreId)"
          >{{ $t("text.delBtn") }}
        </el-button>
      </template>
    </avue-crud>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="refreshChange"
    ></add-or-update>
  </div>
</template>

<script>
import { tableOption } from '@/crud/live/liveProdStore'
import AddOrUpdate from './liveProdStore-add-or-update'

export default {
  data () {
    return {
      dataList: [],
      shopNum: 0,
      totalNum: 0,
      deleteShopNum: 0,
      deleteTotalNum: 0,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {}, // 搜索
      dataListLoading: false,
      tableOption: tableOption,
      resourcesUrl: window.SITE_CONFIG.resourcesUrl,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList (page) {
      this.dataListLoading = true
      this.getAddProdNum()
      this.$http({
        url: this.$http.adornUrl('/live/liveProdStore/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize
            },
            this.searchForm
          )
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 获取剩余可新增得数量
    getAddProdNum () {
      this.$http({
        url: this.$http.adornUrl('/live/liveProdStore/getAddProdNum'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.shopNum = data.shopNum
        this.totalNum = data.totalNum
        this.deleteShopNum = data.deleteShopNum
        this.deleteTotalNum = data.deleteTotalNum
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    /**
     * 提交审核
     */
    submitVerify (id) {
      this.$confirm(this.$i18n.t('live.contForReview'), this.$i18n.t('live.submitReview'), {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/live/liveProdStore/submitVerify'),
          method: 'put',
          data: this.$http.adornData({
            liveProdStoreId: id
          })
        }).then(({ data }) => {
          if (data) {
            this.$message({
              message: this.$i18n.t('live.submitReview'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.refreshChange()
              }
            })
          }
        })
      }).catch(() => {
      })
    },
    deleteHandle (id) {
      this.$confirm(this.$i18n.t('live.consumeRemainingTimes') + this.deleteShopNum + this.$i18n.t('live.times') + ',' + this.$i18n.t('admin.isDeleOper'), this.$i18n.t('text.tips') + '?', {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/live/liveProdStore/' + id),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => {
      })
    },
    /**
     * 刷新回调
     */
    refreshChange () {
      this.page.currentPage = 1
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.page.currentPage = 1
      this.searchForm = params
      this.getDataList(this.page)
    }
  }
}
</script>
<style lang="scss">
.mod-live-liveProdStore {
  .prod-image {
    display: flex;
    align-items: center;
  }

  .prod-image .name {
    margin-left: 10px;
    line-height: 20px;
  }
}
</style>
